/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Master
 */
public class DAO_PhanCong {
    public List<POJO_PhanCong> LoadList() throws SQLException{
        DataProvider data = new DataProvider();
        data.Open();
        
        List<POJO_PhanCong> list = new ArrayList<>();
        String query = "select * from phan_cong";
        ResultSet rs = data.ExecuteQuery(query);
        
        while(rs.next()){
            POJO_PhanCong phanCong = new POJO_PhanCong();
            
            phanCong.setMachuyen(rs.getString("MaChuyen"));
            phanCong.setManv(rs.getString("MaNV"));
            phanCong.setVaitro(rs.getString("VaiTro"));
            
            list.add(phanCong);
        }
        return list;
    }
    
    public void Insert(POJO_PhanCong phanCong){
        DataProvider data = new DataProvider();
        data.Open();
        
        String query = String.format("INSERT INTO `phan_cong`(`MaNV`, `MaChuyen`, `VaiTro`) VALUES ('%s','%s','%s')", phanCong.getManv(), phanCong.getMachuyen(), phanCong.getVaitro());
        
        data.ExeCuteUpdate(query);
    }
    
    public void Update(POJO_PhanCong phanCong){
        DataProvider data = new DataProvider();
        data.Open();
        
        String query = String.format("UPDATE `phan_cong` SET `MaNV`='%s',`MaChuyen`='%s',`VaiTro`='%s' WHERE MaNV = '%s'", phanCong.getManv(), phanCong.getMachuyen(), phanCong.getVaitro(), phanCong.getManv());
        
        data.ExeCuteUpdate(query);
    }
    
    public void Delete(POJO_PhanCong phanCong){
        DataProvider data = new DataProvider();
        data.Open();
        
        String query = String.format("DELETE FROM `phan_cong` WHERE MaNV = '%s'", phanCong.getManv());
        
        data.ExeCuteUpdate(query);
    }
}
